package com.sgb.kjwl.viewmodel.self.pick;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.InviteComEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.model.entity.user.UserAddressEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.pick.InviteComActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class InviteComModel extends BaseViewModel {

    public ObservableField<UserAddressEntity> userAddressEntity = new ObservableField<>(new UserAddressEntity());
    private final InviteComActivity activity;
    private final SelfRepertory repertory;
    private int num = 1;
    private int size = 20;


    public InviteComModel(InviteComActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void getInviteOfferList(String no, String district, boolean isLoadMore) {
        DialogHelper.showProgressDialog(activity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        num = isLoadMore ? num + 1 : 1;
        APIManager.getInstance().getPickAPI().getInviteOfferList(no, getAreaCode(), district, num, size).enqueue(new Callback<BaseEntity<ListResponse<List<InviteComEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<InviteComEntity>>>> call, Response<BaseEntity<ListResponse<List<InviteComEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || !response.body().getState().equals("ok")) return;
                activity.showList(response.body().getData().getList(), isLoadMore);
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<InviteComEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                activity.showList(null, isLoadMore);
            }
        });

    }

    /**
     * 邀请报价
     * <p>
     * companyNoList 被邀请公司的noList，多个no用 "," 隔开
     * projectNo 项目no
     * conditions 邀请报价条件
     */
    public void inviteOffer(String comList, String no, String conditions) {
        APIManager.getInstance().getPickAPI().inviteOffer(comList, no, conditions).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                if (response.body() == null || !response.body().getState().equals("ok")) {
                    MToast.showToast(activity, "邀请失败");
                    return;
                } else {
                    MToast.showToast(activity, response.body().getData().toString());
                    activity.getInviteList(false);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {

            }
        });

    }


    public void getInviteBidList(String no, String district, boolean isLoadMore) {
        DialogHelper.showProgressDialog(activity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        num = isLoadMore ? num + 1 : 1;
        APIManager.getInstance().getPickAPI().getInviteBidList(no, getAreaCode(), district, num, size).enqueue(new Callback<BaseEntity<ListResponse<List<InviteComEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<InviteComEntity>>>> call, Response<BaseEntity<ListResponse<List<InviteComEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || !response.body().getState().equals("ok")) return;
                activity.showList(response.body().getData().getList(), isLoadMore);
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<InviteComEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }

    public void inviteBid(String comList, String no, String conditions) {
        APIManager.getInstance().getPickAPI().inviteBid(comList, no, conditions).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                if (response.body() == null || !response.body().getState().equals("ok")) {
                    MToast.showToast(activity, "邀请失败");
                    return;
                } else {
                    MToast.showToast(activity, response.body().getData().toString());
                    activity.getInviteList(false);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {

            }
        });

    }

    /**
     * 所有地址选项
     */
    public void getAddressModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                activity.initAddressData(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public String getAreaCode() {
        return repertory.getAreaCode();
    }
}
